/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof.wget;

import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;

/**
 *
 * @author Carlos Pinheiro
 */
public class ParsersController extends Thread
{

    private static ParsersController pc = new ParsersController();
    private static final int WAIT_TIME = 1000;

    private ParsersController()
    {
    }

    public static ParsersController getInstance()
    {
        return pc;
    }

    @Override
    public void run()
    {
        ParsersLauncher pl = ParsersLauncher.getInstance();
        OperatingSystemMXBean mx = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double cpuUsage, maxCpuUsage;

        try
        {
            while (!isInterrupted())
            {
                /*if (ToParseQueue.getInstance().isEmpty())
                {
                    pl.removeAllParsers();
                }*/
                /*else if (pl.getNumberOfParsers() == 0)
                {
                    pl.increaseParsers();
                }*/
                /*else
                {*/
                    maxCpuUsage = pl.getMaxCpuUsage();
                    cpuUsage = mx.getSystemCpuLoad();
                    if (cpuUsage < maxCpuUsage)
                    {
                        pl.increaseParsers();
                    }
                    else
                    {
                        pl.decreaseParsers();
                    }

                    Thread.sleep(WAIT_TIME);
                //}
            }
        }
        catch (InterruptedException ex)
        {
        }
    }
}
